package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sky.constant.BusinessConstant.SETMEAL;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2023/4/9 21:25
 */
@Api(tags = "套餐模块")
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = SETMEAL,key = "#setmealDTO.categoryId")
    @PostMapping
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐：{}",setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @ApiOperation("分页查询")
    @GetMapping("page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO pageQueryDTO){
        log.info("分页查询：{}",pageQueryDTO);
        PageResult pageResult = this.setmealService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除")
    @CacheEvict(cacheNames = SETMEAL,allEntries = true)
    @DeleteMapping
    public Result batchDelete(@RequestParam List<Long> ids){
        log.info("批量删除：{}",ids);
        this.setmealService.batchDelete(ids);
        return Result.success();
    }

    @ApiOperation("根据ID查询套餐")
    @GetMapping("{id}")
    public Result<SetmealVO> queryById(@PathVariable Long id){
        log.info("根据ID查询套餐：{}",id);
        SetmealVO setmealVO = this.setmealService.queryById(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = SETMEAL,allEntries = true)
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐：{}",setmealDTO);
        this.setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("启售停售")
    @CacheEvict(cacheNames = SETMEAL,allEntries = true)
    @PostMapping("status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启售停售：{},{}",id,status);
        this.setmealService.startOrStop(id,status);
        return Result.success();
    }
}
